package com.pattern.design.architect.decorator;

/**
 * @author LIN
 * @date 2023-02-18 17:41
 */

public class DecoratorDemo {

    public static void main(String[] args) {
        IDecorator test = new AbstractDecorator();
        IDecorator concreteDecorator = new ConcreteDecorator(test);
        ConcreteDecorator concreteDecorator1 = new ConcreteDecorator(concreteDecorator);
        ConcreteDecorator concreteDecorator2 = new ConcreteDecorator(concreteDecorator1);

        concreteDecorator2.sayHello();
        ConcreteDecorator concreteDecorator3 = new ConcreteDecorator(test) {
            @Override
            public void sayHello() {
                System.out.println("最外层上部");
                super.sayHello();
                System.out.println("最外层下部");
            }
        };
        concreteDecorator3.sayHello();

    }

    interface IDecorator {

        void sayHello();
    }
    static class AbstractDecorator implements IDecorator{

        @Override
        public void sayHello() {
            System.out.println("test");
        }
    }

    static class ConcreteDecorator extends AbstractDecorator {

        private IDecorator iDecorator;

        public ConcreteDecorator(IDecorator iDecorator){
            this.iDecorator = iDecorator;
        }
        @Override
        public void sayHello() {
            System.out.println("say hello begin");
            iDecorator.sayHello();
            System.out.println("say hello end");
        }
    }
}
